package com.example.myblog.service;

import com.example.myblog.dao.BlogMapper;
import com.example.myblog.entity.Blog;
import com.example.myblog.entity.Tag;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class BlogService {

    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private TagService tagService;

    /**
     *前num个推荐博客
     */
    public List<Blog> topRecNum(Integer num){
        return blogMapper.topRecNum(num);
    }
    /**分页查询 */
    public Page<Blog> listBlog(int pageNum,Integer pageSize){
        pageSize = pageSize==null?3:pageSize;
        PageHelper.startPage(pageNum,pageSize);
        return blogMapper.findBlogAll();
    }
    /**按条件（map）分页查询 */
    public Page<Blog> listByCondition(Map<String,Object> map, Integer page){
        PageHelper.startPage(page,3);
        Page<Blog> result = blogMapper.findBlogByCondition(map);
        for (Blog blog : result) {
            setBlogTag(blog);
        }
        return result;
    }
    /**展示title或content包含搜索关键字的blog*/
    public Page<Blog> listSearch(String searchParam,Integer page){
        PageHelper.startPage(page,5);
        return blogMapper.findBlogBySearch(searchParam);
    }
    /**查询当前分类下前num篇博客--page*/
    public List<Blog> topColNum(String type,Integer num){
        return blogMapper.topColNum(type,num);
    }
    /**根据id查询*/
    public Blog findById(Integer id){
        Blog blog = blogMapper.findBlogById(id);
        setBlogTag(blog);
        return blog;
    }
    /**根据年份查询*/
    public List<Blog> listByYear(String year){
        return blogMapper.findByYear(year);
    }
    /**查询blog篇数*/
    public Integer countBlog(){return blogMapper.countBlog();}
    /**增 */
    public int save(Blog blog){
        blog.setCreateTime(new Timestamp(new Date().getTime()));
        blog.setPubTime(new Timestamp(new Date().getTime()));
        if (blog.getId()!=null){
            return blogMapper.updateBlog(blog);
        }
        blog.setViews(0);
        return blogMapper.addBlog(blog);
    }
    /**改 */
    public int update(Blog blog){
        return blogMapper.updateBlog(blog);
    }
    /**删 */
    public int deleteById(Integer id){
        return blogMapper.deleteBlogById(id);
    }

    //util
    private void setBlogTag(Blog blog){
        List<Tag> tagList = tagService.listByNames(blog.getBlogTags());
        if (tagList!=null){
            blog.setBlogTag(tagList);
            StringBuilder blogTagIds = new StringBuilder();
            for (int i = 0; i < tagList.size(); i++) {
                Integer id = tagList.get(i).getId();
                if (id!=null){
                    blogTagIds.append(id);
                }
                if (i < tagList.size()-1) {
                    blogTagIds.append(',');
                }
            }
            blog.setBlogTagIds(blogTagIds.toString());
        }
    }


}
